//
//  ViewController.m
//  类似淘宝详情页
//
//  Created by songhuipan on 16/6/26.
//  Copyright © 2016年 songhuipan. All rights reserved.
//
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#import "ViewController.h"
#import "GoodsDetailController.h"
#import <MJRefresh/MJRefresh.h>
#import "UIView+PullView.h"
static NSString *const cellId = @"cell";
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.tableView];
    
    [self registerCell];
    
    
    // 商品详情页
    GoodsDetailController *detailController = [[GoodsDetailController alloc]init];
    [self addChildViewController:detailController];
    detailController.view.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight);
    [self.scrollView addSubview:detailController.view];
    
    self.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
            [self.view addpullView];
        } completion:^(BOOL finished) {
            [self.scrollView.mj_header endRefreshing];
        }];
    }];
}
- (void)registerCell {
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
}
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        _scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight*2);
        _scrollView.pagingEnabled = YES;
    }
    return _scrollView;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            NSLog(@"%@",self.navigationController.navigationBar);
//            [self.navigationController setNavigationBarHidden:YES];
            [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
                [self.scrollView setContentOffset:CGPointMake(0, kScreenHeight) animated:NO];
            } completion:^(BOOL finished) {
//                [self.navigationController setNavigationBarHidden:NO];
                [self.tableView.mj_footer endRefreshing];
            }];
        }];
        [footer setTitle:nil forState:MJRefreshStateIdle];
        [footer setTitle:@"松开，马上加载图文详情" forState:MJRefreshStateRefreshing];
        [footer setTitle:@"松开，马上加载图文详情" forState:MJRefreshStateWillRefresh];
        [footer setTitle:@"松开，马上加载图文详情" forState:MJRefreshStatePulling];
        
        
        
        [footer setActivityIndicatorViewStyle:1];
        footer.stateLabel.textColor = [UIColor redColor];
        _tableView.mj_footer = footer;
        // 这里设置tableView的下拉刷新不管用，需要设置scrollView的下拉刷新
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            
//            [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
//                [self.view addpullView];
//            } completion:^(BOOL finished) {
//                [self.tableView.mj_header endRefreshing];
//            }];
//        }];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    cell.textLabel.text = @(indexPath.row).stringValue;
    return cell;
}
@end











